<?php
require '/config.php';
require '/include.php';

$rowMenu = catMenu();

// Header Cache infomation
header('Content-Type: text/html; charset=UTF-8');
header('Expires: ' . EXPRIRES);
header('Last-Modified: ' . EXPRIRES);
header("Cache-Control: private, must-revalidate");
header('Cache-Control: max-age=' . MAXAGE);

// Get value
$games = isset($_GET['game']) ? strval($_GET['game']) : NULL;
if ($games)
	$games = strtolower($games);

// Select game
$Qry = 'SELECT *
		FROM games
		WHERE active = 1 AND g_url = "' . htmlentities($games) . '"';

if ($db->numRows($Qry) > 0) {
	// page status
	$status = true;
	// result
	$row = $db->fetchAll($Qry);

	// Fix Game name
	$game_name = mb_convert_case($row[0]['g_name'], MB_CASE_TITLE, 'UTF-8');

	// Sub categories
	$category = $row[0]['category'];
	$category = explode(',', $category);
	$parrent = $category[0];

	// Parrent category
	$Qry = 'SELECT c_name, c_url
			FROM categories
			WHERE c_url = "' . $parrent . '"';
	$parrent_nav = $db->fetchAll($Qry);
	$parrent_breadcrumb = '<li><a href="' . control_cat . $parrent_nav[0]['c_url'] . '" title="' . $parrent_nav[0]['c_name'] . '">' . $parrent_nav[0]['c_name'] . '</a></li>';
	// Assign
	$smarty->assign('parrent_breadcrumb', $parrent_breadcrumb);

	// Tags
	$for_search = '';
	if ($row[0]['tags']) {
		$tags = explode(',', $row[0]['tags']);
		$tags = array_filter($tags);

		$tags_breadcrumb = '';

		foreach ($tags as $key=>$value) {
			// For search similar games
			$for_search .= 'tags LIKE "%' . $value . '%" OR ';
			/*----------------------------*/
			$Qry = 'SELECT tag, name
					FROM cat
					WHERE tag = "' . $value . '"';
			if ($db->numRows($Qry) > 0) {
				$res = $db->fetchRow($Qry);
				$tags_breadcrumb .= '<li><a href="' . control_tag . $res[0] . '" title="' . $res[1] . '">' . $res[1] . '</a></li>';
			}
		}

		$for_search = rtrim($for_search, ' OR');
		// Assign
		$smarty->assign('tags_breadcrumb', $tags_breadcrumb);
	} else {
		// For search similar games
		$for_search .= 'category LIKE "%' . $parrent . '%"';
	}

	// Sub categories
	$Qry = 'SELECT name, tag, icon
			FROM cat
			WHERE parrent = "' . $parrent . '"
			ORDER BY name ASC';
	$subcat = $db->fetchAll($Qry);

	$tags = array();
	foreach ($subcat as $key=>$value){
		$imgpath = DC_ROOT . '/data/icon/' . $value['icon'];

		if ($value['icon'] && file_exists($imgpath))
			$value['icon'] = '/data/icon/' . $value['icon'];
		else
			$value['icon'] = '/static/img/' . 'icon.jpg';

		$tags[] = $value;
	}

	// Assign sub categories
	$smarty->assign('sub_categories', $tags);

	// Assign game name
	$smarty->assign('game_is_name', $game_name);

	// SEO - Summary
	if ($row[0]['summary'])
		$page_description = sprintf($lang['game_summary'], $game_name, $row[0]['summary']);
	else
		$page_description = sprintf($lang['game_description'], $game_name);

	$page_title = sprintf($lang['game_title'], $game_name);
	$page_keywords = sprintf($lang['game_keywords'], $game_name, $game_name, $game_name, $game_name, $game_name);

	$image_src = rtrim(BASE_URL, '/') . '/data/uploads/' . $row[0]['images'];
	$base_url = rtrim(BASE_URL, '/') . $_SERVER['REQUEST_URI'];
	// End SEO

	// Check File 'extension'
	$file_parts = pathinfo($row[0]['object']);

	if (isset($file_parts['extension'])) {
		$file_extension = $file_parts['extension'];
		$pos = strpos($file_extension, '?');

		if ($pos !== false){
			$ext = explode('?', $file_extension);
			$extension = $ext[0];
		}else
			$extension = $file_parts['extension'];
	}

	if (isset($extension)){
		if ($extension == 'swf' or $extension == 'dcr' or $extension == 'unity3d')
			$game_type = $extension;
		else
			$game_type = 'iframe';
	}else
		$game_type = 'iframe';

	// Fix width height
	if ($row[0]['width'] >= 800) {
		$row[0]['height'] = ceil(($row[0]['height'] * 750)/$row[0]['width']);
		$row[0]['width'] = 750;
	}

	if ($mobile) {
		$row[0]['width'] = '100%';
// 		$row[0]['height'] = 300;
	}

	// fix image
	$imgpath = DC_ROOT . '/data/uploads/' . $row[0]['images'];

	if ($row[0]['images'] && file_exists($imgpath))
		$row[0]['images'] = '/data/uploads/' . $row[0]['images'];

	// Similar games
	$Qry = 'SELECT g_name, g_url, images, MATCH(g_name) AGAINST ("' . $row[0]['g_name'] . '" IN BOOLEAN MODE) AS score
			FROM games
			WHERE active = 1 AND g_id != ' . $row[0]['g_id'] . ' AND (' . $for_search . ') AND MATCH(g_name) AGAINST ("' . $row[0]['g_name'] . '")
			ORDER BY score DESC
			LIMIT ' . similar_limit;
	$result = $db->fetchAll($Qry);

	$similargames = array();
	foreach ($result as $key=>$value){
		$imgpath = DC_ROOT . '/data/uploads/' . $value['images'];

		if ($value['images'] && file_exists($imgpath))
			$value['images'] = '/data/uploads/' . $value['images'];
		else
			$value['images'] = '/static.jpg';

		$similargames[] = $value;
	}
	$smarty->assign('similargames', $similargames);

	// related games
	if ($for_search) {
		$Qry = 'SELECT g_name, g_url, images
				FROM games
				WHERE active = 1 AND (' . $for_search . ')
				ORDER BY RAND()
				LIMIT ' . similar_limit;
		$result = $db->fetchAll($Qry);

		$relatedgames = array();
		foreach ($result as $key=>$value){
			$imgpath = DC_ROOT . '/data/uploads/' . $value['images'];

			if ($value['images'] && file_exists($imgpath))
				$value['images'] = '/data/uploads/' . $value['images'];
			else
				$value['images'] = '/static.jpg';

			$relatedgames[] = $value;
		}
		$smarty->assign('relatedgames', $relatedgames);
	}

	// Rating
	$buttonRate = buttonSet($row[0]['g_id']);
	$smarty->assign('buttonRate', $buttonRate);

	// Update played
	gamePlayed($row[0]['g_id']);
} else {
	header('HTTP/1.1 404 Not Found');

	$page_title = '404 - Page not found - ' . $lang['page_title'];
	$page_description = '404 - Page not found - ' . $lang['page_description'];
	$page_keywords = $lang['page_keywords'];
	$image_src = '';
	$base_url = '';

	$status = false;
}

// Option
$smarty->assign('og_type', true);
$smarty->assign('is_category', true);
$smarty->assign('status', $status);

if ($status) {
	$smarty->assign('play_now', true);
	$smarty->assign('row', $row);
	$smarty->assign('game_type', $game_type);
}

// SEO
$smarty->assign('page_title', $page_title);
$smarty->assign('page_keywords', $page_keywords);
$smarty->assign('page_description', $page_description);
$smarty->assign('image_src', $image_src);
$smarty->assign('base_url', $base_url);

// display theme using smarty
$smarty->display(SKIN . '/play.tpl');